package controllers

import (
	"github.com/astaxie/beego"
	"crawl_movie/models"
	"github.com/astaxie/beego/httplib"
	"time"
	"fmt"
)

type CrawlMovieController struct {
	beego.Controller
}

func (c *CrawlMovieController) CrawlMovie() {

	var movieInfo models.MovieInfo

	//连接redis
	models.ConnectReids("localhost:6379")

	//爬虫入口url
	sUrl := "https://movie.douban.com/subject/25827935/"
	models.PutinQueue(sUrl)

	for{
		//首先判断队列的长度，看是否为空
		queueLen := models.GetQueueLength()
		if queueLen == 0 {
			break
		}

		//队列里有东西，取出最后一个
		sUrl = models.PopfromQueue()
		//判断是否已经访问过此连接，避免重复抓爬
		if models.IsVisited(sUrl) {
			continue
		}

		//没有抓爬过的网址，通过httplib爬下网页代码
		rsp := httplib.Get(sUrl)
		sMovieHtml,err := rsp.String()

		if err!= nil {
			panic(err)
		}
		//c.Ctx.WriteString("<br>" + sUrl + "</br>")

		//分析网页代码，看有没有电影名
		movieInfo.Movie_name = models.GetMovieName(sMovieHtml)
		//如果有电影名，则继续取出所有相关项，并存入数据库
		if movieInfo.Movie_name != ""{
			//movieInfo.Movie_name = models.GetMovieName(sMovieHtml)
			movieInfo.Movie_director = models.GetMovieDirector(sMovieHtml)
			movieInfo.Movie_main_character = models.GetMovieMainCharacters(sMovieHtml)
			movieInfo.Movie_type = models.GetMovieGenre(sMovieHtml)
			movieInfo.Movie_on_time = models.GetMovieOnTime(sMovieHtml)
			movieInfo.Movie_grade = models.GetMovieGrade(sMovieHtml)
			movieInfo.Movie_span = models.GetMovieRunningTime(sMovieHtml)

			_,err :=models.AddMovie(&movieInfo)
			if err != nil {
				fmt.Println(err)
			}
		}

		//没有电影名或者其他情况，查找此网页中的所有电影连接，保存至数组
		urls := models.GetMovieUrls(sMovieHtml)

		//将保存的连接逐一加入队列
		for _,urls := range urls{
			models.PutinQueue(urls)
			c.Ctx.WriteString("<br>" + urls + "</br>")
		}

		//将已经抓爬过的连接保存至数组，避免重复抓爬
		models.AddToSet(sUrl)

		time.Sleep(time.Second)
	}
}